trigger CaseAutoReplyCheck on Case (before insert)
{
    // get a list of the offending email vectors, those that have been 
    // seen more than 5 times in the last 5 mins.
    //
    EmailVelocityHandler evh = new EmailVelocityHandler();
    list<EmailVelocityHandler.Hist> off = evh.handle(5, 5);
    List<EmailVelocityIgnore__c> blackList = EmailVelocityIgnoreHandler.getBlackList();
    List<CaseBlackList__c> caseBlacklist = CaseBlacklistHandler.getAutocloseActive();
    
    String msg = 'Case email velocity trigger: number of offenders in window = ' + off.size();
    System.debug(Logginglevel.INFO, msg);
    
    // add the blacklisted email addresses to the offenders list
    for(EmailVelocityIgnore__c blitem : blackList)
    {
        EmailVelocityHandler.Hist bl = new EmailVelocityHandler.Hist();          
        bl.toAddress = blitem.ToAddress__c;
        bl.fromAddress = blitem.FromAddress__c;
        bl.cnt = 0;        
        off.add(bl);        
    }
    
    
    try
    {   
        for (Case cs : Trigger.new)
        {
           if (cs.SuppliedEmail != null)
           {      
                for (EmailVelocityHandler.Hist offender : off)
                {
                    if (cs.SuppliedEmail.equalsIgnoreCase(offender.toAddress))
                    {    
                        String wrn = 'Setting NoAutoReply on case ' + cs.Id + ' created in response to ' + cs.SuppliedEmail;
                        System.debug(Logginglevel.INFO, msg);
                        cs.NoAutoReplyDate__c = datetime.now();
                    }
                }
                for (CaseBlackList__c bl : caseBlacklist)
                {
                    if (cs.SuppliedEmail.equalsIgnoreCase(bl.Email__c ))
                    {
                        String wrn = 'Setting Status to Closed on case ' + cs.Id + ' created in response to ' + cs.SuppliedEmail;
                        System.debug(Logginglevel.INFO, msg);
                        cs.Status = 'Closed';
                    }
                }
           }     
        }
    }
    catch (Exception e)
    {
        String emsg = 'Case email velocity trigger exception: ' + e.getMessage();
        System.debug(Logginglevel.ERROR, msg);
    }
}